< previous page page_186 next page >

Page 186
8160-0186a.gif
Figure 9.3
A call by reference.
Although the keyword ByRef is actually optional in front of the declaration of the arguments, I recommend putting it in because it makes the code much clearer to read.
I also recommend avoiding the use of ByRef all together, because its difficult to track down a bug where youre expecting a local variable not to change and it does. You have to spend time walking through each of the procedures called to find out where the local variable is getting changed. This is a terrible waste of time. Its much better to not let procedures change their arguments. This way, you avoid these problems totally.
Of course, there are times when you need to get more than one value back from a function. Because a function can only return one value, you may need to pass a parameter by reference so that you can get two different values back. Consider the following code:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cmdFuncByRef_Click ()
   Dim intChars As Integer
   Dim strValue As String

   intChars = StringChange(strValue)

   MsgBox strValue
End Sub

 
< previous page page_186 next page >

If you like this book, buy it!